# Create your views here.
import urllib, urllib2
import os, stat, time, sha, sys
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
import django.utils.simplejson as jsondata

#from xml2dict import fromstring
from activitystream.lib.xml2dict import fromstring
import urlparse
from TweetData import TweetData

def render(template, payload):    
    return render_to_response(template, payload)

def index(request):
    tData = TweetData()
    userTimeLine = tData.getUserTimeLine()
    friendsData = tData.getFriendsDetail()
    favoritesData = tData.getFavorites()
    #return HttpResponse("Tweets : %s." % favoritesData)

    payload = dict(tweetData = userTimeLine, friendData = friendsData, favorites = favoritesData, userName = tData.username, tags = tData.getHashTag())
    return render('tweet.html', payload)

def index_1(request):
    
    keyword = 'ayojak'
    #url = 'http://www.slideshare.net/api/2/get_slideshows_by_tag?api_key='
    #url += api_key+'&ts='+str(ts)+'&hash='+hash_key+'&username='+username+'&password='+password+'&tag=ayojak'
    
    #params = urllib.urlencode({'api_key': api_key, 'ts': ts, 'hash': hash_key, 'username': username, 'password': password, 'tag': tag})
    
    
    #url = 'http://search.twitter.com/search.json?q=%40'+keyword
    import sys
    try:
        url = 'http://twitter.com/direct_messages.json'
        
        auth_handler = urllib2.HTTPBasicAuthHandler()
        (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(url)
        auth_handler.add_password('Twitter API', netloc, 'signure', 'sign!meSign1n')
        opener = urllib2.build_opener(auth_handler)
        # ...and install it globally so it can be used with urlopen.
        urllib2.install_opener(opener)
       
        f = urllib2.urlopen(url)
        json = f.read()
        
        #return HttpResponse("Tweets : %s." % json)
    
        #import re    
        #new_str = re.sub('[$]', '', json)
        #json1 = jsondata.loads(json)
        json1 = jsondata.JSONDecoder().decode(json)
    except:
        return HttpResponse("Unexpected error:", sys.exc_info()[0])
        raise

    if not json:
        import sys
        print >> sys.stderr, 'No response. Perhaps slideshare down?'
        sys.exit(1)
    payload = dict(tweetdata = json1)
    return render('tweet.html', payload)

def tweetSearch(request):
    keyword = 'ayojak'
    url = 'http://search.twitter.com/search.json?q=%40'+keyword
    
    f = urllib2.urlopen(url)
    json = f.read()
    json1 = jsondata.JSONDecoder().decode(json)
    
    if not json:
        import sys
        print >> sys.stderr, 'No response. Perhaps slideshare down?'
        sys.exit(1)
    payload = dict(tweetdata = json1)
    return render('tweet-search.html', payload)

def tweetFavorites(request):
    import sys
    try:
        url = 'http://twitter.com/favorites.json'
        
        auth_handler = urllib2.HTTPBasicAuthHandler()
        (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(url)
        auth_handler.add_password('Twitter API', netloc, 'ayojak_event', 'logical')
        opener = urllib2.build_opener(auth_handler)
        # ...and install it globally so it can be used with urlopen.
        urllib2.install_opener(opener)
       
        f = urllib2.urlopen(url)
        json = f.read()        
      
        json1 = jsondata.JSONDecoder().decode(json)
    except:
        return HttpResponse("Unexpected error:", sys.exc_info()[0])
        raise

    if not json:
        import sys
        print >> sys.stderr, 'No response. Perhaps slideshare down?'
        sys.exit(1)
    payload = dict(tweetdata = json1)
    return render('tweet.html', payload)